package com.felix.tests;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import felix.things.router.modbus.mapping.TypeTagMapping;
import felix.things.router.modbus.response.ModBusDataProcessor;
import felix.things.router.mqtt.MqttUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static felix.things.router.modbus.ModbusConstants.BIG_ENDIAN_BYTE_ORDER;

/**
 * @author Felix
 * @date 2019/10/31
 **/
@Component
@Slf4j
public class ModBusDataPrint implements ModBusDataProcessor {
    @Autowired
    private MqttUtils mqttUtils;

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public List<TypeTagMapping> getMappings() {
        // TODO 读取excel配置tag
        ArrayList<TypeTagMapping> mappings = new ArrayList<>();
        mappings.add(new TypeTagMapping("test", Long.class, 0, 1, BIG_ENDIAN_BYTE_ORDER));
//        mappings.add(new TypeTagMapping("test", 0));
        return mappings;
    }

    @Override
    public void processorData(String tag, HashMap<String, Object> response, int start, int end) {
        response.forEach((s, o) -> {
            // 有符号位转无符号位
            long l = Integer.parseInt(o.toString()) & 0x0FFFFL;
            log.info("key:{},无符号位value:{},有符号位value:{}", s, l, o.toString());
        });
    }

    @Override
    public String getTag() {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return "tcp:qinghe";
    }
}
